﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ICWB
    {
         public DataSet CWB_GetList(out int ItemCount, int CandidateID, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateWB_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CandidateID", CandidateID);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public CWBInfo CWB_GetInfo(int _CWB_ID)
        {
            CWBInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateWB_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CWB_ID", _CWB_ID);
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CWBInfo();
                    retVal.ID = Convert.ToInt32(dr["CWB_ID"]);
                    retVal.CandidateID = Convert.ToInt32(dr["CWB_CandidateID"]);
                    retVal.CompanyName = Convert.ToString(dr["CWB_CompanyName"]);
                    retVal.Position = Convert.ToString(dr["CWB_Position"]);
                    retVal.EndDate = dr["CWB_StartDate"] == DBNull.Value ? DateTime.MaxValue : Convert.ToDateTime(dr["CWB_StartDate"]);
                    retVal.EndDate = dr["CWB_EndDate"] == DBNull.Value ? DateTime.MaxValue : Convert.ToDateTime(dr["CWB_EndDate"]);
                    retVal.City = Convert.ToString(dr["CWB_City"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int CWB_Insert(CWBInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateWB_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@CWB_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CWB_CompanyName", eInfo.CompanyName);
            dbCmd.Parameters.AddWithValue("@CWB_Position", eInfo.Position);
            if (eInfo.StartDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CWB_StartDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@CWB_StartDate", eInfo.StartDate);
            if (eInfo.EndDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CWB_EndDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@CWB_EndDate", eInfo.EndDate);
            dbCmd.Parameters.AddWithValue("@CWB_City", eInfo.City);


            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CWB_Update(CWBInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateWB_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CWB_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@CWB_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CWB_CompanyName", eInfo.CompanyName);
            dbCmd.Parameters.AddWithValue("@CWB_Position", eInfo.Position);
            if (eInfo.StartDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CWB_StartDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@CWB_StartDate", eInfo.StartDate);
            if (eInfo.EndDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CWB_EndDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@CWB_EndDate", eInfo.EndDate);

            dbCmd.Parameters.AddWithValue("@CWB_City", eInfo.City);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CWB_Delete(int CWBID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateWB_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CWB_ID", CWBID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CWB_Exist(int _ID, int _CandidateID, string _No)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateWB_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CWB_ID", _ID);
            dbCmd.Parameters.AddWithValue("@CWB_CandidateID", _CandidateID);
            dbCmd.Parameters.AddWithValue("@CWB_CompanyName", _No);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
